package com.googlecode.springgameengine.lwjgl;

import com.googlecode.springgameengine.actor.MeshActor;
import com.googlecode.springgameengine.rendering.ActorRenderer;
import com.googlecode.springgameengine.resource.ResourceLoader;
import com.googlecode.springgameengine.resource.ResourceNotLoadableException;
import org.lwjgl.opengl.ARBShaderObjects;
import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Maciej
 */
public class LWJGLMeshActorRenderer implements ActorRenderer<MeshActor> {

	private ResourceLoader resourceLoader;

	@Override
	public void render(MeshActor actor) throws ResourceNotLoadableException {
		
		LWJGLMesh meshResource = (LWJGLMesh) resourceLoader.load(actor.getMesh());
		int meshList = meshResource.getMeshList();
		LWJGLShaderMaterial shaderMaterial = (LWJGLShaderMaterial) resourceLoader.load(actor.getMaterial());
		
		ARBShaderObjects.glUseProgramObjectARB(shaderMaterial.getShader());
		glLoadIdentity();
		glRotatef(actor.getPitch(), 1, 0, 0);
		glRotatef(actor.getYaw(), 0, 1, 0);
		glRotatef(actor.getRoll(), 0, 0, 1);
		glTranslatef(-actor.getX(), -actor.getY(), -actor.getZ());

		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glCallList(meshList);
	}

	public ResourceLoader getResourceLoader() {
		return resourceLoader;
	}

	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}
}
